##############################
# Analysis of survey data
##############################

rm(list=ls())

library(foreign)
library(Hmisc)
library(ri)
library(stargazer)
library(readstata13)
library(Cairo)
library(svglite)
library("ggplot2")
library(gridExtra)
library(ggpubr)
library(grid)

##############################
# Prepare data
##############################

# load data
d = read.dta13("~/Dropbox/Cerrillos 2017/08_replication/survey_cerrillos_LAPS.dta")
table(d$cuadrante)

# explore data
names(d)
head(d)

# Ideological labels
table(d$a5, exclude = NULL)
d$ideolabels = "no ideology"
d$ideolabels[d$a5<5]="left"
d$ideolabels[d$a5>4 & d$a5 < 7]="center"
d$ideolabels[d$a5>6 & d$a5 < 11]="right"
table(d$ideolabels,d$a5)
prop.table(table(d$ideolabels))

# Ideologia
table(d$a5, exclude = NULL)
prop.table(table(d$a5))
d$ideology = 0
d$ideology[d$a5<11]=1
table(d$ideology)
prop.table(table(d$ideology))

# Heuristico izquierda 
table(d$c1, exclude = NULL)
table(d$c1)[1]/nrow(d)

d$policyleft = "DK/DA"
d$policyleft[d$c1==1]="Left"
d$policyleft[d$c1==2]="Right"
d$policyleft[d$c1==3]="None"
table(d$policyleft)

d$policyleft_correct = 0
d$policyleft_correct[d$policyleft=="Left"]=1
prop.table(table(d$policyleft_correct))

d$policyleft = as.factor(d$policyleft)
d$policyleft = factor(d$policyleft,levels(d$policyleft)[c(2,4,3,1)])
table(d$policyleft)

g_left <- ggplot(d) + stat_count(mapping = aes(x=policyleft, y=..prop.., group=1))

f_left <- ggplot(d) + stat_count(mapping = aes(x=policyleft, y=..prop.., group=1)) + 
          labs(x = "Ideological labels", y = "Proportion", title = "") +
          theme(plot.title = element_text(hjust = 0.5)) + 
          theme(axis.text=element_text(size=15)) + 
          theme_grey(base_size = 15) + ggtitle("Social welfare policies") + 
          theme(plot.title = element_text(size=15, hjust = 0.5)) + 
          scale_y_continuous(breaks=c(0,0.1,0.2,0.3,0.4,0.5))

# Heuristico derecha
table(d$c2, exclude = NULL)
table(d$c2)[2]/nrow(d)

d$policyright = "DK/DA"
d$policyright[d$c2==1]="Left"
d$policyright[d$c2==2]="Right"
d$policyright[d$c2==3]="None"
table(d$policyright)

d$policyright = as.factor(d$policyright)
d$policyright = factor(d$policyright,levels(d$policyright)[c(2,4,3,1)])
table(d$policyright)

d$policyright_correct = 0
d$policyright_correct[d$policyright=="Right"]=1
prop.table(table(d$policyright_correct))

f_right <- ggplot(d) + stat_count(mapping = aes(x=policyright, y=..prop.., group=1)) + 
           labs(x = "Ideological labels", y = "Proportion", title = "") +
           theme(plot.title = element_text(hjust = 0.5)) + 
           theme(axis.text=element_text(size=15)) + 
           theme_grey(base_size = 15) + ggtitle("Iron-fist crime-reduction policies") + 
           theme(plot.title = element_text(size=15, hjust = 0.5)) + 
           scale_y_continuous(breaks=c(0,0.1,0.2,0.3,0.4,0.5))

# Figure 1
tiff('~/Dropbox/Cerrillos 2017/08_replication/figure1.tiff', units="in", width=10, height=5, res=300, compression = 'lzw')
ggarrange(f_left,f_right)
dev.off()

